package com.house.rent.ui.fragment;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.base.lib.mvp.present.BasePresenter;
import com.google.android.material.tabs.TabLayout;
import com.house.rent.R;
import com.house.rent.ui.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @date 2019-10-23 15:05
 * @desc 房源页
 */
public class MainFragment extends BaseFragment {

    @BindView(R.id.tb_main)
    TabLayout mTabLayout;
    @BindView(R.id.vp_main)
    ViewPager mViewPager;

    List<Fragment> mFragments = new ArrayList<>();

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initTop() {
        setTitle("房源信息");
        enableRight("搜索", v -> SearchActivity.start(mContext));
    }

    @Override
    protected void initView() {
        mFragments.add(HouseFragment.newInstance(0));
        mFragments.add(HouseFragment.newInstance(3));
        mFragments.add(HouseFragment.newInstance(1));
        mFragments.add(HouseFragment.newInstance(2));

        mViewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected void initListeners() {

    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        private final String[] TITLE = {
                "出售",
                "出租",
                "求购",
                "求租",
        };

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position];
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        }
    }
}
